package com.ehsy.statement;

import com.dtflys.forest.http.ForestRequest;
import com.dtflys.forest.http.ForestResponse;
import com.ehsy.service.RedirectService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;

/**
 * @author YanZhen_Li
 * @version 1.0
 * @title A13重定向
 * @description
 * @create 2024/6/18 21:21
 */
@SpringBootTest
        (webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
                properties = {"spring.config.location=classpath:redirect.yml"})
public class A13重定向 {
    @Resource
    RedirectService service;
    /**
     * 重定向测试
     */
    @Test
    public void test1() {
        String test1 = service.test1("生日快乐");
        String test2 = service.test2("生日快乐");
        System.out.println(test1);
        System.out.println(test2);
    }

    /**
     * 使用 ForestResponse 处理重定向
     */
    @Test
    public void test2() {
        ForestResponse<String> test3 = service.test3("生日快乐");
        // 判断是否需要重定向跳转
        if (test3.isRedirection()) {
            // 获得即将跳转的请求对象
            ForestRequest redirectReq = test3.redirectionRequest();
            // 如有需要，可以对添加请求进行修改
            // redirectReq.addBody("foo", "bar");
            // 执行跳转
            String test4 = redirectReq.executeAsString();
            System.out.println(test4);
        }
    }
}
